{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "ZwAPANLEe1D2"
   },
   "outputs": [],
   "source": [
    "import scipy.io"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Hdq8gLyPe1D5"
   },
   "outputs": [],
   "source": [
    "PA100k_annotation_fp = \"./data/PA100k/annotation.mat\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "adOL_MHXe1D8"
   },
   "outputs": [],
   "source": [
    "PA100k_annotation_dict = scipy.io.loadmat(PA100k_annotation_fp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Kwjure52e1D-"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['__header__', '__version__', '__globals__', 'attributes', 'test_images_name', 'test_label', 'train_images_name', 'train_label', 'val_images_name', 'val_label'])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PA100k_annotation_dict.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "VEMT4-ZNe1EC"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Female',\n",
       " 'AgeOver60',\n",
       " 'Age18-60',\n",
       " 'AgeLess18',\n",
       " 'Front',\n",
       " 'Side',\n",
       " 'Back',\n",
       " 'Hat',\n",
       " 'Glasses',\n",
       " 'HandBag',\n",
       " 'ShoulderBag',\n",
       " 'Backpack',\n",
       " 'HoldObjectsInFront',\n",
       " 'ShortSleeve',\n",
       " 'LongSleeve',\n",
       " 'UpperStride',\n",
       " 'UpperLogo',\n",
       " 'UpperPlaid',\n",
       " 'UpperSplice',\n",
       " 'LowerStripe',\n",
       " 'LowerPattern',\n",
       " 'LongCoat',\n",
       " 'Trousers',\n",
       " 'Shorts',\n",
       " 'Skirt&Dress',\n",
       " 'boots']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "attribute_list = [x[0][0] for x in PA100k_annotation_dict[\"attributes\"]]\n",
    "attribute_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "H507Z3mJe1EE"
   },
   "outputs": [],
   "source": [
    "attribute_dict = {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Yrt2GhWqe1EH"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       1, 0, 0, 0], dtype=uint8)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PA100k_annotation_dict[\"test_label\"][10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "code_folding": [],
    "colab": {},
    "colab_type": "code",
    "id": "QD1c0uTFe1EJ"
   },
   "outputs": [],
   "source": [
    "instances = PA100k_annotation_dict[\"test_label\"]\n",
    "for instance_attribute_list in instances:\n",
    "    for i in range(len(instance_attribute_list)):\n",
    "        if instance_attribute_list[i] == 1:\n",
    "            value = attribute_list[i]\n",
    "        elif instance_attribute_list[i] == -1:\n",
    "            value = \"No_\" + attribute_list[i]\n",
    "        else:\n",
    "            value = \"Unspecified\"\n",
    "\n",
    "        # Count the number of times that value has appeared\n",
    "        if value in attribute_dict.keys():\n",
    "            attribute_dict[value] += 1\n",
    "        else:\n",
    "            attribute_dict[value] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "C0sk6aEbe1EL"
   },
   "outputs": [],
   "source": [
    "instances = PA100k_annotation_dict[\"train_label\"]\n",
    "for instance_attribute_list in instances:\n",
    "    for i in range(len(instance_attribute_list)):\n",
    "        if instance_attribute_list[i] == 1:\n",
    "            value = attribute_list[i]\n",
    "        elif instance_attribute_list[i] == -1:\n",
    "            value = \"No_\" + attribute_list[i]\n",
    "        else:\n",
    "            value = \"Unspecified\"\n",
    "\n",
    "        # Count the number of times that value has appeared\n",
    "        if value in attribute_dict.keys():\n",
    "            attribute_dict[value] += 1\n",
    "        else:\n",
    "            attribute_dict[value] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "dKGf72n-e1EN"
   },
   "outputs": [],
   "source": [
    "instances = PA100k_annotation_dict[\"val_label\"]\n",
    "for instance_attribute_list in instances:\n",
    "    for i in range(len(instance_attribute_list)):\n",
    "        if instance_attribute_list[i] == 1:\n",
    "            value = attribute_list[i]\n",
    "        elif instance_attribute_list[i] == -1:\n",
    "            value = \"No_\" + attribute_list[i]\n",
    "        else:\n",
    "            value = \"Unspecified\"\n",
    "\n",
    "        # Count the number of times that value has appeared\n",
    "        if value in attribute_dict.keys():\n",
    "            attribute_dict[value] += 1\n",
    "        else:\n",
    "            attribute_dict[value] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "LI1lQF9-e1EQ"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Unspecified': 2036421,\n",
       " 'Age18-60': 92844,\n",
       " 'Side': 30508,\n",
       " 'Glasses': 18662,\n",
       " 'HandBag': 18115,\n",
       " 'LongSleeve': 43087,\n",
       " 'Trousers': 71916,\n",
       " 'Front': 34707,\n",
       " 'ShortSleeve': 56913,\n",
       " 'UpperLogo': 14835,\n",
       " 'Female': 45336,\n",
       " 'Back': 34785,\n",
       " 'Backpack': 15926,\n",
       " 'UpperPlaid': 10917,\n",
       " 'LowerPattern': 1639,\n",
       " 'Skirt&Dress': 11155,\n",
       " 'UpperStride': 5088,\n",
       " 'ShoulderBag': 19301,\n",
       " 'HoldObjectsInFront': 958,\n",
       " 'Shorts': 16896,\n",
       " 'AgeOver60': 1469,\n",
       " 'LongCoat': 3365,\n",
       " 'UpperSplice': 4219,\n",
       " 'Hat': 4206,\n",
       " 'LowerStripe': 450,\n",
       " 'AgeLess18': 5687,\n",
       " 'boots': 595}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "attribute_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "XilxZ6JFe1ES"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Age18-60 92844\n",
      "AgeLess18 5687\n",
      "AgeOver60 1469\n",
      "Back 34785\n",
      "Backpack 15926\n",
      "Female 45336\n",
      "Front 34707\n",
      "Glasses 18662\n",
      "HandBag 18115\n",
      "Hat 4206\n",
      "HoldObjectsInFront 958\n",
      "LongCoat 3365\n",
      "LongSleeve 43087\n",
      "LowerPattern 1639\n",
      "LowerStripe 450\n",
      "ShortSleeve 56913\n",
      "Shorts 16896\n",
      "ShoulderBag 19301\n",
      "Side 30508\n",
      "Skirt&Dress 11155\n",
      "Trousers 71916\n",
      "Unspecified 2036421\n",
      "UpperLogo 14835\n",
      "UpperPlaid 10917\n",
      "UpperSplice 4219\n",
      "UpperStride 5088\n",
      "boots 595\n"
     ]
    }
   ],
   "source": [
    "# Print out all the counts\n",
    "for key in sorted(attribute_dict):\n",
    "    print(key, attribute_dict[key])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "iwUiVT2qe1EU"
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "name": "Extract PA-100K Attributes.ipynb",
   "private_outputs": true,
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
